package com.ssm.controller;

import com.alibaba.fastjson.JSONArray;
import com.ssm.pojo.Bill;
import com.ssm.pojo.Provider;
import com.ssm.service.bill.BillService;
import com.ssm.service.provider.ProviderService;
import com.ssm.util.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Controller
@RequestMapping("/bill")
public class BillController {
    @Autowired
    @Qualifier("providerServiceImpl")
    private ProviderService providerService;
    @Autowired
    @Qualifier("billServiceImpl")
    private BillService billService;
    @RequestMapping("/toAddBill")
    public String toAddBill(Model model){
        return "billadd";
    }
    @RequestMapping("/list")
    public String toBillList(String queryProductName,Integer queryProviderId,Integer queryIsPayment,Integer pageIndex,Model model){
        PageSupport ps = new PageSupport();
        int billCount = billService.getBillCount(queryProductName, queryProviderId, queryIsPayment);
        if(pageIndex!=null){
            ps.setCurrentPageNo(pageIndex);
        }
        ps.setPageSize(5);/*设置分页大小*/
        ps.setTotalCount(billCount);/*设置记录总数*/
        List<Bill> billList = billService.getBillList(queryProductName, queryProviderId, queryIsPayment, ps.getOffset(), ps.getPageSize());
        List<Provider> providerList = providerService.getAllProvider();
        model.addAttribute("providerList",providerList);
        model.addAttribute("billList",billList);
        model.addAttribute("queryProductName",queryProductName);
        model.addAttribute("queryProviderId",queryProviderId);
        model.addAttribute("queryIsPayment",queryIsPayment);
        model.addAttribute("currentPageNo", ps.getCurrentPageNo());
        model.addAttribute("totalCount", billCount);
        model.addAttribute("totalPageCount", ps.getTotalPageCount());
        return "billlist";
    }

    @RequestMapping("/toBillView")
    public String toBillView(@RequestParam("billid") String id,Model model){
        Bill bill = billService.getBillById(id);
        model.addAttribute("bill", bill);
        return "billview";
    }
    @RequestMapping("/toModifyBill")
    public String toModifyBill(@RequestParam("billid")String id,Model model){
        Bill bill = billService.getBillById(id);
        model.addAttribute("bill", bill);
        return "billmodify";
    }
    @RequestMapping("/modifyBill")
    public void modifyBill(String billCode, String productName, String productUnit, BigDecimal productCount, BigDecimal totalPrice, Integer providerId, Integer isPayment, HttpServletResponse response) throws IOException {
        int row = billService.modifyBill(billCode, productName, productUnit, productCount, totalPrice, providerId, isPayment);
        if (row>0){
            System.out.println("订单修改成功!");
        }else {
            System.out.println("订单修改失败!");
        }
        response.sendRedirect("/bill/list");
    }
    @RequestMapping("/deleteBill")
    @ResponseBody
    public String deleteBill(@RequestParam("billid") String id){
        Bill billById = billService.getBillById(id);
        Map map = new HashMap();
        if (billById==null){
            map.put("result", "not exist");
        }else{
            
            int rowCount = billService.deleteBillById(id);
            if (rowCount>0){
                map.put("result", "true");
                System.out.println("删除订单成功!");
            }else{
                map.put("result", "false");
                System.out.println("删除订单失败!");
            }
        }
        String data = JSONArray.toJSONString(map);
        return data;
    }
}
